<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("basePath",basePath);

String fbh = request.getParameter("fbh");
String fdrillStyle = request.getParameter("fdrillStyle");
String fyear = request.getParameter("fyear");
String fbdate = request.getParameter("fbdate");
String fedate = request.getParameter("fedate");
String ftjlx = request.getParameter("ftjlx");
String foffi_type = request.getParameter("foffi_type");
String foffi_name = request.getParameter("foffi_name");
String fvalue = request.getParameter("fvalue");

//System.out.println("-------"+fbh+"-"+fdrillStyle+"-"+fyear+"-"+fbdate+"-"+fedate+"-"+ftjlx+"-"+foffi_type+"-"+foffi_name+"-"+fvalue+"-");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>首页</title>
    <link rel="stylesheet" type="text/css"
	href="<%=path%>/css/btn_class.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/extjs/style.css" />
<script type="text/javascript"
	src="<%=path%>/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=path%>/extjs/ext-all.js"></script>

	<script type="text/javascript" src = "${basePath}/FusionCharts/FusionCharts.js"></script>
	<script type="text/javascript" src = "${basepath}/FusionCharts/FusionChartsExportComponent.js"></script>
	<script type="text/javascript" src = "${basePath}/mould_published/public.js"></script>
	<script type="text/javascript" src="<%=path%>/ext-3.2.0/examples/ux/ColumnHeaderGroup.js"></script>
    <script type="text/javascript" src="<%=path%>/ext-3.2.0/src/locale/ext-lang-zh_CN.js"></script>
    <script type="text/javascript"	src="<%=path%>/js/common_fields.js"></script>
    <script type="text/javascript"	src="${basePath}/mould_published/chart_bbms.js"></script>
	<script type="text/javascript">
	Ext.BLANK_IMAGE_URL = "<%=path%>/extjs/resources/images/default/s.gif";
	
	/*****************************************************************************************************************
	*  统计分析部分       show_chart() 是生成方法； 每一个Ajax调用生成一个图标或报表
	*      钻取依据  -- 日期 1；年度2；季度3；月度4；天5；科室8；       
	*
	******************************************************************************************************************/
	
		var fbh = "<%=fbh%>";
		var fdrillStyle = "<%=fdrillStyle%>";
		var fvalue = "<%=fvalue%>";
		var fbdate = "<%=fbdate%>";
		var fedate = "<%=fedate%>";
		var ftjlx = "<%=ftjlx%>";
		var foffi_type = "<%=foffi_type%>";
		var foffi_name = "<%=foffi_name%>";
		var fyear = "<%=fyear%>";
		
		var fquery_str = "";
		var ftitle = "";
		
		var fdateTemp = "";
		if(fdrillStyle == "1" || fdrillStyle == "2" || fdrillStyle == "3" || fdrillStyle == "4" ||fdrillStyle == "5") {
			if(ftjlx == "yyyy") {
				
			} else if(ftjlx == "q") { //父页面按季度,获取x轴的值就是季度值
				
				if(fvalue == "1") {
					fdateTemp = "fbdate::" + fyear + "-01-01##" + "fedate::" + fyear + "-03-31##";
					fbdate = fyear + "-01-01";
					fedate = fyear + "-03-31";
				} else if(fvalue == "2") {
					fdateTemp = "fbdate::" + fyear + "-04-01##" + "fedate::" + fyear + "-06-30##";
					fbdate = fyear + "-04-01";
					fedate = fyear + "-06-30";
				} else if(fvalue == "3") {
					fdateTemp = "fbdate::" + fyear + "-07-01##" + "fedate::" + fyear + "-09-30##";
					fbdate = fyear + "-07-01";
					fedate = fyear + "-09-30";
				} else {
					fdateTemp = "fbdate::" + fyear + "-10-01##" + "fedate::" + fyear + "-12-31##";
					fbdate = fyear + "-10-01";
					fedate = fyear + "-12-31";
				}
				fquery_str = fdateTemp
		           + "ftjlx::mm##"
		           + "foffi_type::" + foffi_type + "##"
		           + "foffi_name::" + foffi_name;
				ftitle = fyear + "年" + fvalue + "季度";
				
			} else if(ftjlx == "mm") {
				fquery_str = getDateStr(fyear, "" + fvalue) + "##"
		           + "ftjlx::mm##"
		           + "foffi_type::" + foffi_type + "##"
		           + "foffi_name::" + foffi_name;
				ftitle = fyear + "年" + fvalue + "月";
				
				
			} else if(ftjlx == "dd") {
				
			} else { 
				fquery_str = "fbdate::" + fbdate + "##" + "fedate::" + fedate + "##";
		           + "ftjlx::mm##"
		           + "foffi_type::" + foffi_type + "##"
		           + "foffi_name::" + fvalue;
				ftitle = fyear + "年";
			}
		} else {
			fquery_str = "fbdate::" + fbdate + "##" + "fedate::" + fedate + "##"
            + "ftjlx::mm##"
            + "foffi::" + fvalue;
		}
		
		 /**********************************************  开始展示图表     **************************************************/
		 
	
		 Ext.Ajax.request({
			url: 'headInfo!produceTableHead.do',
			method : 'POST',
			async:false,
			params : {
				"fbh" : fbh,
				"fquery_str" : fquery_str
			},
			success : function(response, action) {
				
				
				var fields = [];
				var data = [];
				var groupData = [];
				var columnData = [];
				var data_ds = new Ext.data.Store({
					reader : new Ext.data.JsonReader({
						totalProperty : 'totalProperty',
						root : 'root'
					}, common_fields),
					idProperty : 'threadid',
					remoteSort : true,
					url: 'headInfo!produceTableData.do'
				});
				
				data_ds.load({
					params : {
							"fbh" : fbh,
							"fquery_str" : fquery_str
						}
				});
				
				
				var data1 = new Array(); //定义一数组,接收全部返回值
				var responseText = response.responseText;
				
				/** responseText格式: 出院病人,3##门诊病人,1@@痊愈,f1##未愈,f2##死亡,f3##门诊,f4
					
					非叶子部分@@叶子部分
					 	非叶子部分: 第一层表头@第二层表头@第三层表头@...
					 		每一层表头部分: header值@##colspan值
					 	叶子部分: header值@##dataIndex值	
				*/
				data1 = responseText.split("@@"); //分割表头和出数列      
				
				//处理表头非叶子部分
				var data2 = new Array(); //定义一数组,接收全部返回值
				var data2_str = data1[0].replace(/\r\n/g,'');;
				data2 = data2_str.split("@"); 
				
				for(var i = 0; i < data2.length; i++) {
					var data2_temp = new Array(); //定义一数组,接收全部返回值
	 				var data2_temp_str = data2[i].replace(/\r\n/g,'');;
	 				var gData = [];
	 				
	 				data2_temp = data2_temp_str.split("##"); //分割表头和出数列
	 				var floor_temp = "[";
	 				for(var j = 0; j < data2_temp.length; j++) {
	 					var head2 = new Array(); //定义一数组,接收全部返回值
	 	 				var head2_str = data2_temp[j].replace(/\r\n/g,'');
	 	 				head2 = head2_str.split(","); //分割表头和出数列
	 	 				//gData.push({ header: head2[0], colspan:head2[1], align:'center'});
	 	 				floor_temp += "{ header:'" + head2[0] + "',colspan:" + head2[1] + ", align:'center'}";
	 	 				if(j == data2_temp.length - 1) {
	 	 					floor_temp += "]";
	 	 				} else {
	 	 					floor_temp += ",";
	 	 				}
	 				}
	 				var obj_floor = Ext.decode(floor_temp);
	 				groupData.push(obj_floor);
				}
				
				
				
				//处理出数列部分（叶子）
				var data3 = new Array(); //定义一数组,接收全部返回值
				var data3_str = data1[1].replace(/\r\n/g,'');
				data3 = data3_str.split("##"); //分割表头和出数列      
				
				for(var j = 0; j < data3.length; j++) {
					var head3 = new Array(); //定义一数组,接收全部返回值
	 				var head3_str = data3[j].replace(/\r\n/g,'');
	 				head3 = head3_str.split(","); //分割表头和出数列
	 				
	 				columnData.push({ header: head3[0], dataIndex:head3[1]});
	 				//alert(head3[1]);
				}
				
				//alert(groupData);
			    var group = new Ext.ux.grid.ColumnHeaderGroup({
			        rows:  groupData
			    });
			    
			    
			    var btn_exp_excel = new Ext.Button({
					text : '导出Excel',
					iconCls : 'sy-white_excel',
					handler : function(btn) {
						var fsdate_value = "";//统计开始日期
						var fedate_value = "";//统计截止日期
						var obj_value = "";
						var temp_str = "";
						for(var i = 0; i < sql_ds.getCount(); i++) {
							//赋值统计日期
							if(sql_ds.getAt(i).data.fname == "fbdate") {
								fsdate_value = sql_grid.store.getById(sql_ds.getAt(i).data.fdesc).get("value").format("Y-m-d");
							}
							if(sql_ds.getAt(i).data.fname == "fedate") {
								fedate_value = sql_grid.store.getById(sql_ds.getAt(i).data.fdesc).get("value").format("Y-m-d");
							}
							temp_str = sql_grid.store.getById(sql_ds.getAt(i).data.fdesc).get("value");
							
							obj_value += sql_ds.getAt(i).data.fname + "::";
							if(temp_str != null && temp_str != "") {
								if(sql_ds.getAt(i).data.ftype == "3") {
									obj_value += temp_str.format("Y-m-d");
								} else {
									obj_value += temp_str;
								}
							} else {
								obj_value += "null";
							}
							obj_value += "##";
						}
						obj_value = obj_value.substring(0, obj_value.length - 2);
						
						Ext.Ajax.request({
							url : 'headInfo!exportReportData.do',
							method : 'POST',
							params : {
								"fbh" : fbh,
								"fquery_str" : obj_value,
								"fstartDate" : fsdate_value,
								"fendDate" : fedate_value
								
							},
							success : function(response, action) {
								var data_url = "../../dataFiles/" + response.responseText + ".xls";
								window.open(data_url,"excel","height=100,width=400"); 
							},
							// 提交失败的回调函数
							failure : function() {
								Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
							}
						});
					}
				});
			    
			    
			    var btn_row_chart = new Ext.Button({
					text : '横向比对分析',
					iconCls : 'sy-row_fx',
					handler : function(btn) {
						var fsdate_value = "";//统计开始日期
						var fedate_value = "";//统计截止日期
						var obj_value = "";
						var temp_str = "";
						window.parent.close();
						//tbfx_row_fun('MRD1002', data_grid, fsdate_value, fedate_value, obj_value);
					}
				});
			    
			     
			    
			    var data_grid = new Ext.grid.GridPanel({
			        //title: 'Sales By Location',
			        width: 1000,
			        height: 400,
			        ds : data_ds,
			        layout : 'fit',
			        region: "center",
            	    stripeRows : true,
			        columns: columnData,
			        bbar:new Ext.PagingToolbar({   
	                    pageSize:1000,   
	                    store: data_ds,   
	                    displayInfo:true,   
	                    displayMsg:' 一共{2}条',   
	                    emptyMsg:"没有记录"   
                	}),
			        viewConfig: {
			            forceFit: false
			        }
			    //	,
			      //  plugins: group
			    });


			    Ext.onReady(function() {
					
					//统计分析panel
				  	var tjfx_panel = new Ext.Panel({
				    	//region: "center",
						//layout:'fit',
						border : false,
					    //title:'&nbsp;&nbsp;&nbsp;图表示例',
					    //collapsible : true,
					    autoScroll : true,
						margins:'0 5 0 0',
					 	
						items : [data_grid]
					});
					
					
				  	var index_panel = new Ext.Panel({
						region : 'center',
						resizeTabs : true,
						id : 'index_panel_id',
						frame: false,
						border: false,
						baseCls:'x-plain',
						layout: 'fit',
						cls: 'topCls',
						layoutConfig : {
							padding: 0
						},
						defaults:{
							margins:'3 0 0 0'
						},
						items  : [tjfx_panel]
					});
				  	
				  	//初始化面板
				  	var viewport = new Ext.Viewport({
			            layout:'border',
			            items:[data_grid]
				  	});
				  	//调用生成图表方法,要在viewport之后才行,不然获取不到id,不显示内容
				  	
				  	
				});
			    
			},
			// 提交失败的回调函数
			failure : function() {
				Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
			}
	  });
	 
	
	/******************************************************************************************
	**                               以下是构成面板部分  包括按钮
	******************************************************************************************/

    
	</script>
  </head>
  <body>
  	 
     
  </body>
</html>
